package com.bonc.excel.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * job log report helper
 *
 * @author xuxueli 2019-11-22
 *
 * 1.统计日志报告信息 (xxl_job_log_report)
 * 2.清理日志详情 (xxl_job_info)
 */
public class JobLogReportHelper {
    private static Logger logger = LoggerFactory.getLogger(JobLogReportHelper.class);

    private static JobLogReportHelper instance = new JobLogReportHelper();
    public static JobLogReportHelper getInstance(){
        return instance;
    }


    private Thread logrThread;
    private volatile boolean toStop = false;
    public void start(){
        logrThread = new Thread(new Runnable() {

            @Override
            public void run() {
                while (!toStop) {
                    // 1、log-report refresh: refresh log report in 3 days
                    try {
                        logger.info("run");
                    } catch (Exception e) {
                        if (!toStop) {
                            logger.error("log report thread error:{}", e);
                        }
                    }
                    try {
                        //休眠一分钟
                        TimeUnit.MINUTES.sleep(1);
                    } catch (Exception e) {
                        if (!toStop) {
                            logger.error(e.getMessage(), e);
                        }
                    }

                }
                logger.info(" log report thread stop");

            }
        });
        logrThread.setDaemon(true);
        logrThread.setName(" admin JobLogReportHelper");
        logrThread.start();
    }

    public void toStop(){
        toStop = true;
        // interrupt and wait
        logrThread.interrupt();
        try {
            logrThread.join();
        } catch (InterruptedException e) {
            logger.error("InterruptedException", e);
        }
    }

}
